package com.github.wxiaoqi.security.common.rest;

import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-15 8:48
 */
@Slf4j
public abstract class BaseController<Entity> {
//    @Autowired
//    protected HttpServletRequest request;
    protected abstract BaseBiz<Entity> getBaseBiz();

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ObjectRestResponse<Entity> add(@RequestBody Entity entity){
    	getBaseBiz().insertSelective(entity);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ObjectRestResponse<Entity> get(@PathVariable int id){
        ObjectRestResponse<Entity> entityObjectRestResponse = new ObjectRestResponse<>();
        Entity o = getBaseBiz().selectById(id);
        entityObjectRestResponse.data(o);
        return entityObjectRestResponse;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ObjectRestResponse<Entity> update(@RequestBody Entity entity){
    	getBaseBiz().updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>();
    }
    
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ObjectRestResponse<Entity> remove(@PathVariable int id){
    	getBaseBiz().deleteById(id);
        return new ObjectRestResponse<Entity>();
    }
    
    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public ObjectRestResponse<Entity> delete(@RequestBody Entity entity){
    	getBaseBiz().delete(entity);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public List<Entity> all(){
        return getBaseBiz().selectListAll();
    }
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return getBaseBiz().selectByQuery(query);
    }
    protected String getCurrentUserName(){
        return BaseContextHandler.getUsername();
    }
}
